package com.bruan.controller;

import com.bruan.anno.MyLog;
import com.bruan.pojo.Dept;
import com.bruan.pojo.Result;
import com.bruan.service.DeptService;
import com.bruan.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.message.ReusableMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
/**
 * 部门管理Controller
 */
@RestController
@RequestMapping("/depts")
public class DeptController {
    //日志
//    private static Logger log = LoggerFactory.getLogger(DeptController.class);
//    依赖注入
    @Autowired
    private DeptService deptService;

    @GetMapping
//    @RequestMapping(value = "/depts" ,method = RequestMethod.GET)//限定请求方式
    public Result list() {
        log.info("查询全部部门对象");
        //调用service查询部门数据
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    /**
     * 删除部门
     */
    @MyLog
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门:{}",id);
        deptService.delete(id);
        //调用service删除部门
        return Result.success();
    }
    /**
     * 新增部门
     */
    @MyLog

    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("添加新的部门：{}",dept.toString());
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result search(@PathVariable Integer id){
        log.info("根据id查询部门{}", id);
        Dept dept =  deptService.search(id);
        return Result.success(dept);
    }

    @MyLog
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门");
        deptService.upadte(dept);
        return Result.success("修改部mop 门成功"+dept.getName());
    }


}
